package com.cola.springmvc.dao;

import com.cola.springmvc.pojo.Address;
import com.cola.springmvc.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map;

/**
 * @Grup:com.cola.springmvc.dao
 * @Auther:cola
 * @Data:2021/4/27-2021-04-22:34
 * @version:1.0
 */

@Repository
public class UserDao {
    private static Map<Integer, User> users=null;

    static {
        users=new HashMap<Integer, User>();
        users.put(1001,new User(1001,"cola1","123","aa@aadad.com",12,new Address(101,"江西","吉安")));
        users.put(1002,new User(1002,"cola2","123","aa@aadad.com",2,new Address(102,"江西","吉安")));
        users.put(1003,new User(1003,"cola3","123","aa@aadad.com",1,new Address(103,"江西","吉安")));
        users.put(1004,new User(1004,"cola4","123","aa@aadad.com",12,new Address(104,"江西","吉安")));
    }
    private static Integer initId=1005;
    public void save(User user){
        if (user.getId()==null){
            user.setId(initId);
            initId++;
            //System.out.println(initId);
        }
        users.put(user.getId(),user);

    }
    public Collection<User> getAll(){
        return users.values();
    }
    public User get(Integer id){
        return users.get(id);
    }

    public void delete(Integer id){
        users.remove(id);
    }
}
